package ctn.service.impl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import ctn.contant.FunContant;
import ctn.contant.PlugContant;
import ctn.dao.hibernate.impl.CPAInfoDaoImpl;
import ctn.dao.hibernate.impl.SpecDaoImpl;
import ctn.dao.inteface.CPAFunDao;
import ctn.dao.inteface.CPAVerDao;
import ctn.hibernate.po.CpaFun;
import ctn.hibernate.po.CtnCpaInfo;
import ctn.hibernate.po.CtnCpaSpec;
import ctn.hibernate.po.CtnCpaVer;
import ctn.service.dto.FunDTO;
import ctn.service.inteface.FunService;
import ctn.util.CPAUtil;
import ctn.util.CTNUserUtil;
import ctn.util.StringUtils;
import ctn.util.jmesa.DateUtils;
import ctn.util.jmesa.PaginationCriteria;
@Service
@Transactional
public class FunServiceImpl implements FunService {

	@Autowired
	private CPAFunDao cPAFunDaoImpl;
	
	@Autowired
	private CPAInfoDaoImpl cPAInfoDaoImpl;
	
	@Autowired
	private CPAVerDao cPAVerDaoImpl;
	@Autowired
	private SpecDaoImpl specDaoImpl;
	@Autowired
	private CPAUtil cpaUtil;
	@Override
	public Long getWfunListCount(PaginationCriteria criteria) {
		return cPAFunDaoImpl.getplugListCountByCriteria(criteria);
	}

	@Override
	public List<FunDTO> getWfunList(PaginationCriteria criteria) {
		return cPAFunDaoImpl.getWfunListByCriteria(criteria);
	}

	@Override
	public void saveFun(FunDTO funDTO) {
		CtnCpaInfo cpaIfo= cPAInfoDaoImpl.queryPlug(funDTO.getCpaID());
		CtnCpaVer  cpaVer= cPAVerDaoImpl.queryVer(funDTO.getVerId());
		CpaFun fun= new CpaFun();
		FunContant.copyDtoToFun( funDTO, cpaIfo,cpaVer, fun);
		fun.setStage(0L);
		cPAFunDaoImpl.saveFun(fun);
		if (funDTO.getUpFile()!= null) {
			CommonsMultipartFile uf = funDTO.getUpFile();
			CtnCpaSpec spec=new CtnCpaSpec();
			spec.setSpecTyp(PlugContant.SPEC_TYPE_REQUIREMENT);
			spec.setCreateDate(new Date());
			spec.setCtnCpaVer(cpaVer);
			spec.setCpaFun(fun);
			spec.setCtnUser(CTNUserUtil.getUser());
			spec.setFileName(uf.getOriginalFilename());
			specDaoImpl.saveCpaSpec(spec);
			String filepath=cpaUtil.getSpecFilePath(spec);
			String folderPath=filepath.replace(uf.getOriginalFilename(), "");
			File folder=new File(folderPath);
			File file=new File(filepath);
			if(!folder.exists()){
				folder.mkdirs();
			}
			BufferedOutputStream os = null;
			BufferedInputStream is = null;
			try {
				os = new BufferedOutputStream(new FileOutputStream(file));
				is = new BufferedInputStream(uf.getInputStream());

				byte[] byteA;
				int read = 0;
				while (read != -1) {
					byteA = new byte[1024];
					read = is.read(byteA);
					if (read != -1) {
						os.write(byteA, 0, read);
					}
				}
				os.flush();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (os != null) {
					try {
						os.close();
					} catch (Exception ex) {
					}
				}
			}
		
		}
	}
	
	@Override
	public void editFun(FunDTO funDTO) {
		CpaFun fun=cPAFunDaoImpl.getCpaFunByID(funDTO.getID());
		if(!StringUtils.isEmptyString(funDTO.getPlanEndDateStr())){
			try {
				fun.setPlanEndDate(DateUtils.parse("yyyy-MM-dd", funDTO.getPlanEndDateStr()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}	
		if (funDTO.getUpFile()!= null) {
			CommonsMultipartFile uf = funDTO.getUpFile();
			CtnCpaSpec spec=new CtnCpaSpec();
			spec.setSpecTyp(PlugContant.SPEC_TYPE_REQUIREMENT);
			spec.setCreateDate(new Date());
			spec.setCtnCpaVer(fun.getCtnCpaVer());
			spec.setCpaFun(fun);
			spec.setCtnUser(CTNUserUtil.getUser());
			spec.setFileName(uf.getOriginalFilename());
			specDaoImpl.saveCpaSpec(spec);
			String filepath=cpaUtil.getSpecFilePath(spec);
			String folderPath=filepath.replace(uf.getOriginalFilename(), "");
			File folder=new File(folderPath);
			File file=new File(filepath);
			if(!folder.exists()){
				folder.mkdirs();
			}
			BufferedOutputStream os = null;
			BufferedInputStream is = null;
			try {
				os = new BufferedOutputStream(new FileOutputStream(file));
				is = new BufferedInputStream(uf.getInputStream());

				byte[] byteA;
				int read = 0;
				while (read != -1) {
					byteA = new byte[1024];
					read = is.read(byteA);
					if (read != -1) {
						os.write(byteA, 0, read);
					}
				}
				os.flush();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (os != null) {
					try {
						os.close();
					} catch (Exception ex) {
					}
				}
			}
		
		}
	}

	@Override
	public FunDTO getWfun(Long id) {
		
		return cPAFunDaoImpl.getFunDTOByID(id);
	}

	@Override
	public void delFun(Long id) {
		// TODO Auto-generated method stub
		
	}



}
